package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import beans.Musica;

public class MusicaDao {

    private static List<Musica> musica = null;

    public static List<Musica> getMusica() {
        if (musica == null) {
            Session session = SimpleSessionFactory.getSessionFactory(null).openSession();
            Query q = session.createQuery("from beans.Musica");

            musica = q.list();
            session.close();

        }
        return musica;
    }

    public static boolean saveOrUpdateOrDelete(List<Musica> musicas, List<Musica> musicasABorrar) {
        Session session = null;
        try{
            session = SimpleSessionFactory.getSessionFactory(null).openSession();

            session.beginTransaction();

            for(Musica musica : musicas)
                session.saveOrUpdate(musica);

            for(Musica musica : musicasABorrar)
                session.delete(musica);

            session.getTransaction().commit();

            return true;
        }catch(Exception ex){
            if(session != null & session.isOpen())
                session.getTransaction().rollback();

            ex.printStackTrace();

            return false;
        }finally{
            if(session != null & session.isOpen())
                session.close();
        }
    }
}
